package com.tianpengtech.front.mobile.passport;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.U;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ggd on 2017/7/25 0025.
 */
public class MobilePassportController extends BaseController{

    private static MobilePassportService ps = MobilePassportService.me;

    /**
     * 保存或更新个人信息
     */
    @Before({MobilePassportValidator.class,Tx.class})
    public void saveOrUpdate() throws Exception {
        Map<String,String> param  = new HashMap<>();
        param.put("honners",getPara("honners"));
        param.put("xqah",getPara("xqah"));
        param.put("scly",getPara("scly"));
        param.put("photo",getPara("photo"));
        param.put("nick_name",getPara("nick_name"));
        param.put("gender",getPara("gender"));
        param.put("age",getPara("age"));
        param.put("province",getPara("province"));
        param.put("city",getPara("city"));
        param.put("id",getPara("id"));
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        param.put("uid",user.get(User.ID));
        try {
            R r=ps.saveOrUpdate(param);
            renderJson(r);
        }catch (Exception ex){
            renderJson(R.fail("失败"));
            throw ex;
        }
    }

    /**
     * 获取兴趣爱好列表
     */
    public void getXQAH(){
        R r=ps.getXQAH();
        renderJson(r);
    }

    /**
     * 获取擅长领域列表
     */
    public void getSCLY(){
        R r=ps.getSCLY();
        renderJson(r);
    }

    /**
     * 获取网红详情
     */
    public void getFairInfo(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        if (user==null){
            renderJson(R.fail("用户不存在"));
            return;
        }
        R r=ps.getFairInfo(user.getLong(User.ID)+"");
        renderJson(r);
    }

    /**
     * 设置或修改交易密码
     */
    public void setTranPwd(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        String old_pwd=getPara("old_pwd");
        String new_pwd=getPara("new_pwd");
        if (StrKit.isBlank(old_pwd) && StrKit.isBlank(new_pwd)){
            renderJson(R.fail("密码不能为空"));
            return;
        }
        long id = user.getLong(User.ID);
        R r=ps.setTranPwd(id,old_pwd,new_pwd);
        renderJson(r);
    }

    /**
     * 验证旧交易密码
     */
    public void checkTranPwd(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        String old_pwd=getPara("old_pwd");
        if (StrKit.isBlank(old_pwd)){
            renderJson(R.fail("密码不能为空"));
            return;
        }
        long id = user.getLong(User.ID);
        R r=ps.checkTranPwd(id,old_pwd);
        renderJson(r);
    }

    /**
     * 保存认证信息
     */
    @Before(MobileFairAuthValidator.class)
    public void saveAuth(){
        Map<String,String> param  = new HashMap<>();
        param.put("user_name",getPara("user_name"));
        param.put("nick_name",getPara("nick_name"));
        param.put("fans",getPara("fans"));
        param.put("screenshot",getPara("screenshot"));
        param.put("type",getPara("type"));
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        param.put("uid",user.getLong(User.ID)+"");
        R r=ps.saveAuth(param);
        renderJson(r);
    }

    /**
     * 查找认证信息
     */
    public void findAuth(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.findAuth(user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 获取点击数详情
     * @return
     */
    public void getClickInfo(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.getClickInfo(user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 获取转化数详情
     * @return
     */
    public void getCvsInfo(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.getCvsInfo(user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 个人中心数据
     * @return
     */
    public void center(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.center(user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 获取收益数据
     */
    public void getWallet(){
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.getWallet(user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 验证交易密码是否正确
     */
    public void checkNowTranPwd(){
        String tranPwd = getPara("tranPwd");
        if (StrKit.isBlank(tranPwd)){
            renderJson(R.fail("交易密码不能为空"));
            return;
        }
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        R r=ps.checkTranPwd(tranPwd,user.getLong(User.ID));
        renderJson(r);
    }

    /**
     * 提现
     */
    @Before(Tx.class)
    public void withdraw(){
        String money = getPara("money");
        String bankId = getPara("bankId");
        String wdToken = getPara("wdToken");
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        if (StrKit.isBlank(wdToken)){
            renderJson(R.fail("wdToken不能为空"));
            return;
        }
        if (StrKit.isBlank(bankId)){
            renderJson(R.fail("bankId不能为空"));
            return;
        }
        if (StrKit.isBlank(money)){
            renderJson(R.fail("money不能为空"));
            return;
        }
        if (!U.isMoney(money)){
            renderJson(R.fail("提现金额格式不正确"));
            return;
        }
        R r=ps.withdraw(money,user.getLong(User.ID),wdToken,bankId);
        renderJson(r);
    }
}
